package com.penghaisoft.microwms.product.web.controller.expose;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.penghaisoft.microwms.common.Constant;
import com.penghaisoft.microwms.common.base.model.BaseFactory;
import com.penghaisoft.microwms.common.controller.BaseController;
import com.penghaisoft.microwms.common.dto.HandResp;
import com.penghaisoft.microwms.common.dto.Resp;
import com.penghaisoft.microwms.common.wms.model.ProOrderInSn;
import com.penghaisoft.microwms.product.service.IBaseFactoryService;
import com.penghaisoft.microwms.product.service.IProOrderInSnService;

/**
 * @Description 成品下线入库明细表
 * @author luoteng
 * @date 2018年6月20日 下午2:22:10 
 * @version 1.0
 */
@RestController
@RequestMapping(value = "/expose/mes")
public class ProExposeMesController extends BaseController {


	@Autowired
	private IProOrderInSnService proOrderInSnService;
	
	@Autowired
	private IBaseFactoryService baseFactoryService;
    
    /** 
     * @Title proOrderInSnAdd
     * @Description 成品下线入库明细表 新增
     * @author luoteng
     * @date 2018年6月20日:下午2:23:02
     * @param list
     * @return HandResp
     */
    @PostMapping("proorderinsn/add")
    public HandResp proOrderInSnAdd(@RequestBody List<ProOrderInSn> list) {
        HandResp result = new HandResp();
        try{
//        	List<ProOrderInSn> checkList = proOrderInSnService.mesBatchInsertOrderInSnCheck(list);
        	String rtnMsg = proOrderInSnService.mesBatchInsertOrderInSnCheck(list);
        	if(rtnMsg != null && !rtnMsg.equals("")){
        		result.setCode(Constant.FAIL);
                result.setMsg(rtnMsg);
                result.setList(list);
        	}else{
//    			是否开启下线即入库（0:停止；1:开启）
    			String isOpenDownlineIn = "0";
//    			默认库位
    			String defaultWlCode = Constant.PRO_DEFAULT_WL_CODE;
    			
        		BaseFactory baseFactory = new BaseFactory();
        		baseFactory.setFactoryCode(Constant.DEFAULT_FACTORY_CODE);
        		baseFactory.setActiveFlag("1");
        		List<BaseFactory> tmpList = baseFactoryService.queryByAny(baseFactory);
        		if(tmpList != null && !tmpList.isEmpty()){
        			BaseFactory tmpBaseFactory = tmpList.get(0);
//        			是否开启下线即入库（0:停止；1:开启）
        			isOpenDownlineIn = tmpBaseFactory.getIsOpenDownlineIn();
//        			默认库位
        			defaultWlCode = tmpBaseFactory.getUserDefined1() == null ? Constant.PRO_DEFAULT_WL_CODE : tmpBaseFactory.getUserDefined1();
        		}
        		
        		if(isOpenDownlineIn.equals("1")){
        			Resp resp = proOrderInSnService.mesBatchInsertOrderInSnAuto(list, defaultWlCode);
                    result.setCode(resp.getCode());
                    result.setMsg(resp.getMsg());
        		}else{
        			Resp resp = proOrderInSnService.mesBatchInsertOrderInSn(list);
                    result.setCode(resp.getCode());
                    result.setMsg(resp.getMsg());
        		}
        	}
        }catch(RuntimeException e){
        	result.setCode(Constant.FAIL);
            result.setMsg(e.getMessage());
            e.printStackTrace();
//            rawRequisitionService.batchNormalHisInsert(list, e.getMessage());
        }
        
        return result;
    }
}
